package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.annotation.LoginUser;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.dto.SmallCanteenPurchaseDTO;
import io.renren.dto.SmallCanteenPurchasemxDTO;
import io.renren.dto.StockOrderDTO;
import io.renren.dto.TemporaryOrderDTO;
import io.renren.entity.UserEntity;
import io.renren.service.SmallCanteenPurchaseService;
import io.renren.service.SmallCanteenPurchasemxService;
import io.renren.service.SysDeptService;
import io.renren.service.TemporaryOrderService;
import io.renren.utils.StringsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 小食堂采购表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@RestController
@RequestMapping("api/smallcanteenpurchase")
@Api(tags="小食堂采购")
public class SmallCanteenPurchaseController {
    @Autowired
    private SmallCanteenPurchaseService smallCanteenPurchaseService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private TemporaryOrderService temporaryOrderService;

    @GetMapping("page")
    @ApiOperation("主厨的订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> page(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<SmallCanteenPurchaseDTO> page = smallCanteenPurchaseService.getOrder(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(2, entity.getId(), entity.getDeptId());
        ArrayList<SmallCanteenPurchaseDTO> arraylist = new ArrayList<>();
        Page page1 = (Page)params.get("page");
        if(1 == page1.getCurrent() && null != temOrder){
            SmallCanteenPurchaseDTO purchaseDTO = new SmallCanteenPurchaseDTO();
            purchaseDTO.setCreateDate(temOrder.getCreateTime());
            purchaseDTO.setMealTime(temOrder.getMealTime());
            purchaseDTO.setFlag(1);
            String deptName = sysDeptService.getDeptName(entity.getDeptId()).getName()+"食堂申请清单草稿";
            purchaseDTO.setChedRemark(deptName);
            arraylist.add(purchaseDTO);
        }
        for (SmallCanteenPurchaseDTO purchaseDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(purchaseDTO.getCanteenId()).getName();
            String str = StringsUtils.getCanteenName(deptName) + "食堂申请清单";
            purchaseDTO.setChedRemark(str);
            arraylist.add(purchaseDTO);
        }
        page.setList(arraylist);
        map.put("list",page);
        SmallCanteenPurchaseDTO total = smallCanteenPurchaseService.getTotal( entity.getId(),null,entity.getDeptId(), 4);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SmallCanteenPurchaseDTO> get(@PathVariable("id") Long id){
        SmallCanteenPurchaseDTO data = smallCanteenPurchaseService.get(id);

        return new Result<SmallCanteenPurchaseDTO>().ok(data);
    }

    /**
     * 生成小食堂采购订单
     * @return
     */
    @PostMapping("saveAuxiliaryOrder")
    @ApiOperation("生成小食堂采购订单")
    public Result saveAuxiliaryOrder(@RequestBody JSONObject params, @LoginUser UserEntity entity){
        smallCanteenPurchaseService.saveAuxiliaryOrder(params,entity);
        return new Result<>();
    }

    @PostMapping("pageAdminUpdate")
    @ApiOperation("食堂管理员审核")
    public Result updateExam(@RequestBody SmallCanteenPurchaseDTO dto, @LoginUser UserEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        return   smallCanteenPurchaseService.updateExamine(dto,entity);

    }
    @PostMapping("UpdateSupplierOrder")
    @ApiOperation("供应商修改订单状态")
    public Result UpdateSupplierOrder(@RequestBody SmallCanteenPurchaseDTO dto, @LoginUser UserEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        return   smallCanteenPurchaseService.UpdateSupplierOrder(dto,entity);

    }
    @GetMapping("/selecSupplierSmallOrder")
    @ApiOperation("供应商的订单列表")
    public Result<Map> selecSupplierSmallOrder(Long supplierId ){
        Map map = smallCanteenPurchaseService.selecSupplierSmallOrder(supplierId);
        return new Result<Map>().ok(map);
    }

    @GetMapping("pageAdmin")
    @ApiOperation("食堂管理员查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
    })
    public Result<Map> pageAdmin(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<SmallCanteenPurchaseDTO> page = smallCanteenPurchaseService.pageAdmin(params);
        HashMap<String, Object> map = new HashMap<>();
        for (SmallCanteenPurchaseDTO purchaseDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(purchaseDTO.getCanteenId()).getName();
            String str = StringsUtils.getCanteenName(deptName) + "食堂申请清单";
            purchaseDTO.setChedRemark(str);
        }
        map.put("list",page);
        SmallCanteenPurchaseDTO total = smallCanteenPurchaseService.getTotal(null,null ,null, 1);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }

    @GetMapping("pagesTorehouseClerk")
    @ApiOperation("仓储员查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
    })
    public Result<Map> pagesTorehouseClerk(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<SmallCanteenPurchaseDTO> page = smallCanteenPurchaseService.pagesTorehouseClerk(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        for (SmallCanteenPurchaseDTO purchaseDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(purchaseDTO.getCanteenId()).getName();
            String str = StringsUtils.getCanteenName(deptName) + "食堂申请清单";
            purchaseDTO.setChedRemark(str);
        }
        map.put("list",page);
        SmallCanteenPurchaseDTO total = smallCanteenPurchaseService.getTotal(null,null ,entity.getDeptId(), 3);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }
/*    @PostMapping("update")
    @ApiOperation("食堂修改")
    public Result update(@RequestBody SmallCanteenPurchaseDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        smallCanteenPurchaseService.update(dto);
        return new Result();

    }*/
}